package com.yll.web;

import com.github.pagehelper.PageInfo;
import com.yll.domain.Student;
import com.yll.domain.User;
import com.yll.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/student")
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    public ModelAndView finAll(Integer pageNum,Integer pageSize,ModelAndView mv){
        int page =1,size=5;
        if (pageNum!=null&&pageSize!=null){
            page=pageNum;
            size=pageSize;
        }
        PageInfo<Student> info = userService.pageQuery(page, size);

            mv.addObject("page",info);
            mv.setViewName("/allStu.jsp");
            return mv;

    }

    @RequestMapping("/findById")
    @ResponseBody
    public ModelAndView findById(Integer id,ModelAndView mv){
        Student student = userService.findById(id);
        if (student!=null){
            mv.addObject("stu",student);
            mv.setViewName("/itemDetail.jsp");
            return mv;
        }else{
            mv.addObject("message_findById","查无此人!");
            mv.setViewName("/student.jsp");
            return mv;
        }
    }


    @RequestMapping("/add")
    @ResponseBody
    public ModelAndView add(Student student, ModelAndView mv){
//        System.out.println("student = " + student);
        userService.add(student);
        mv.setViewName("/student.jsp");
        return mv;
    }

    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView login(String username,String password,ModelAndView mv){
        User user = userService.findByUsernameAndPassword(username, password);
        if (user!=null){
            mv.setViewName("/student.jsp");
            return mv;
        }else{
            mv.addObject("message","用户名或密码错误!");
            mv.setViewName("/index.jsp");
            return mv;
        }
    }
}
